import re
import json
import time
import scrapy
from DrissionPage import ChromiumPage
from bilibili.items import BilibiliItem

class VideoinfoSpider(scrapy.Spider):
    name = "videoinfo"
    allowed_domains = ["bilibili.com"]
    start_urls = ["https://space.bilibili.com/34579852/video"]

    def __init__(self, *args, **kwargs):
        super(VideoinfoSpider, self).__init__(*args, **kwargs)
        self.page = ChromiumPage(timeout=15)

    def parse(self, response):
        self.page.listen.start('api.bilibili.com/x/space/wbi/arc/search')
        try:
            self.page.get(url=response.url, retry=3, interval=2)
            resp = self.page.listen.wait()
            json_data = resp.response.body
            for info in json_data['data']['list']['vlist']:
                bvid = info['bvid']
                url = f'https://www.bilibili.com/video/{bvid}/?spm_id_from=333.999.0.0'
                time.sleep(2)
                yield scrapy.Request(url=url,callback=self.get_data)
        except Exception as e:
            print(e)
        # finally:
        #     self.page.close()

    def get_data(self, response):
        item = BilibiliItem()
        if response:
            # 标题，不能有空格和特殊字符
            title = re.findall('<title data-vue-meta="true">(.*?)哔哩哔哩_bilibili</title>', response.text)[0]
            title = re.findall('[\u4e00-\u9fff]+', title)
            title = ''.join(title)
            # 获取音频和视频
            info = json.loads(re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0])
            video_url = info['data']['dash']['video'][0]['baseUrl']
            audio_url = info['data']['dash']['audio'][0]['baseUrl']
            item['name'] = title
            item['audio_url'] = audio_url
            item['video_url'] = video_url
            yield item
        else:
            print('音视频解析失败')
